﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Shapes;
using DataForest.DataModel;
using DataForest.Utilities;
using DataForest.ViewModel.Dialogs;

namespace DataForest.ViewModel
{
    public class TreeTabModel : TabModel
    {
        public TreeTabModel(Workgroup group, Tree tree) : base (group)
        {
            this.Tree = tree;
            //this.Group.Table.TableNewRow += test;
            this.Group.Table.RowChanged += test;
        }

        public void test(object sender, DataRowChangeEventArgs e)
        {
            StaticSelectedRow = e.Row;
        }

        private Node selectedNode;
        public Node SelectedNode
        {
            get
            {
                return selectedNode;
            }
            set
            {
                if (value != selectedNode) { selectedNode = value; }
                else { selectedNode = null; }
                OnPropertyChanged("SelectedNode");
                OnPropertyChanged("SelectedData");
            }
        }

        public DataView SelectedData
        {
            get
            {
                if (SelectedNode != null)
                {
                    return SelectedNode.Data;
                }
                return this.Group.Table.AsDataView();
            }
        }

        private object selectedRow;
        public object SelectedRow
        {
            get
            {
                return selectedRow;
            }
            set
            {
                if (selectedRow != value)
                {
                    if (value is DataRowView || value is DataRow)
                    {
                        if (value != null)
                        {
                            StaticSelectedRow = value;
                        }
                        selectedRow = value;
                        OnPropertyChanged("SelectedRow");
                    }
                }
            }
        }

        private object staticSelectedRow;
        public object StaticSelectedRow
        {
            get
            {
                return staticSelectedRow;
            }
            protected set
            {
                if (staticSelectedRow != value)
                {
                    staticSelectedRow = value;
                    OnPropertyChanged("StaticSelectedRow");
                }
            }
        }

        private bool isDataVisible;
        public bool IsDataVisible
        {
            get
            {
                return isDataVisible || showRules;
            }
            set
            {
                if (value != isDataVisible)
                {
                    isDataVisible = value;
                    OnPropertyChanged("IsDataVisible");
                }
            }
        }

        private bool showRules;
        public bool ShowRules
        {
            get
            {
                return showRules;
            }
            set
            {
                if (showRules != value)
                {
                    showRules = value;
                    OnPropertyChanged("ShowRules");
                    OnPropertyChanged("IsDataVisible");
                }
            }
        }


        private Tree tree;
        public Tree Tree {
            get
            {
                return tree;
            }
            set
            {
                if (value != tree)
                {
                    tree = value;
                    OnPropertyChanged("Tree");
                }
            }

        }

        private RelayCommand selectNodeCommand;
        public RelayCommand SelectNodeCommand
        {
            get
            {
                if (selectNodeCommand == null)
                {
                    selectNodeCommand = new RelayCommand(param => SelectedNode = param as Node);
                }
                return selectNodeCommand;
            }
        }

        private RelayCommand toggleIsDataVisibleCommand;
        public RelayCommand ToogleIsDataVisibleCommand
        {
            get
            {
                if (toggleIsDataVisibleCommand == null)
                {
                    toggleIsDataVisibleCommand = new RelayCommand(param => IsDataVisible = !IsDataVisible);
                }
                return toggleIsDataVisibleCommand;
            }
        }

        private RelayCommand chooseBestAttributCommand;
        public RelayCommand ChooseBestAttributCommand
        {
            get
            {
                if (chooseBestAttributCommand == null)
                {
                    chooseBestAttributCommand = new RelayCommand(param => ChooseBestAttributHandler(param));
                }
                return chooseBestAttributCommand;
            }
        }

        private RelayCommand changeSplitValueCommand;
        public RelayCommand ChangeSplitValueCommand
        {
            get
            {
                if (changeSplitValueCommand == null)
                {
                    changeSplitValueCommand = new RelayCommand(param => ChangeSplitValueHandler(param));
                }
                return changeSplitValueCommand;
            }
        }

        private RelayCommand setAttributCommand;
        public RelayCommand SetAttributCommand
        {
            get
            {
                if (setAttributCommand == null)
                {
                    setAttributCommand = new RelayCommand(param => SetAttributHandler(param as object[]));
                }
                return setAttributCommand;
            }
        }

        private RelayCommand removeAttributCommand;
        public RelayCommand RemoveAttributCommand
        {
            get
            {
                if (removeAttributCommand == null)
                {
                    removeAttributCommand = new RelayCommand(param => RemoveAttributHandler(param), param => param is Node && (param as Node).Attribut != null);
                }
                return removeAttributCommand;
            }
        }

        private void ChooseBestAttributHandler(object param)
        {
            Node node = param as Node;
            if (node != null)
            {
                node.ChooseBestAttribut();
            }
        }


        private void ChangeSplitValueHandler(object param)
        {
            Node node = param as Node;
            if (node != null && !node.IsDiscret) {
                SplitValueDialogModel model = new SplitValueDialogModel();
                model.SplitValue = node.SplitValue;
                model.MaxSplitValue = node.MaxValue;
                model.MinSplitValue = node.MinValue;
                if (MyDialogService.ShowDialog(model) == true)
                {
                    node.SplitValue = model.SplitValue;
                }
            }
        }

        private void RemoveAttributHandler(object param)
        {
            Node node = param as Node;
            if (node != null)
            {
                node.Attribut = null;
            }
        }

        private void DoSomething(object param)
        {

        }

        private void SetAttributHandler(object[] param)
        {
            if (param == null) { return; }

            Node node = null;
            DataColumn attribut = null;

            foreach (var item in param)
            {
                if (item is Node) { node = item as Node; }
                if (item is DataColumn) { attribut = item as DataColumn; }
            }

            if (node != null && attribut != null)
            {
                node.Attribut = attribut;
            }
        }

    }
}
